google.load('visualization', '1', {'packages':['corechart']});
google.load('visualization', '1', {'packages': ['imagechart']});
  var chart = null;
  var dataBar = null;
  var dataArray=Array(0);
  var BioGraphMap=null;
  var BioGraphMarker=null;


function downloadUrl(url, callback) {
  var request = window.ActiveXObject ?
      new ActiveXObject('Microsoft.XMLHTTP') :
      new XMLHttpRequest;

  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      request.onreadystatechange = doNothing;
      callback(request.responseText, request.status);
    }
  };

  request.open('GET', url, true);
  request.send(null);
}

function SetBioChart(params)
{//alert(clusterdata.length);
   var downloadgraphdata="";
 if(clusterdata.length<=0 ||  $("#panel_detail").is(":visible")==false)
     return;
 $("#GraphBio_Bar").html('<div style="height: 100%; width: 100%; background: url(/images/loading/loading9.gif) 45% 50% no-repeat">  </div>');

delGraphMarker();

var clusterlocations="";
    for(var clusCounter=0; clusCounter<=clusterdata.length-1;clusCounter++)
        {
           if (clusterlocations!="")
               {
                   clusterlocations=clusterlocations+",";
               }
           clusterlocations=clusterlocations+clusterdata[clusCounter].title;
        }

     
    var ul="";
            ul="http://" + location.hostname + "/visualization/Biographdata?search=" + clusterlocations + "&Selectedspecies="+params;
    downloadUrl(url = ul, function(returnVal) {

        var rawData = returnVal.split("|");
        
         dataBar = new google.visualization.DataTable();
         dataBar.addColumn('string', 'Location');
         var chkRow="";
         var counter=-1;
        var cIndex;
        var Dataval;
        var speciessum=0;
        var fntSize=10;
        var txtColor="#5375ab";
        var tmpLocationIds;
        var dataArray=new Array();
        for(var clusCounter=0; clusCounter<=clusterdata.length-1;clusCounter++)
            {if (downloadgraphdata!="")downloadgraphdata=downloadgraphdata+"|";
                tmpLocationIds=clusterdata[clusCounter].title.split(",");
               
                dataBar.addRow();
                if (clusterdata[clusCounter].getMarkers().length==1)
                    {
                        //dataBar.setCell(clusCounter, 0,clusterdata[clusCounter].title+"");
                    }
                else if (clusterdata[clusCounter].getMarkers().length>1)
                    {
                        dataBar.setCell(clusCounter, 0,clusterTitle[clusCounter]);
                        dataArray[clusCounter]=clusterdata[clusCounter].getCenter();
                        downloadgraphdata=downloadgraphdata+clusterTitle[clusCounter];
                        
                       
                    }
                

               for(var rdata=0; rdata<=rawData.length-2;rdata++)
                {
                  Dataval = rawData[rdata].split(":");
                  
                  if(indexofArray(tmpLocationIds,Dataval[0])!= -1)
                      {
                     if (clusterdata[clusCounter].getMarkers().length==1)
                    {
                        dataBar.setCell(clusCounter, 0,Dataval[2]+" (1)");
                        dataArray[clusCounter]=clusterdata[clusCounter].getCenter();
                        downloadgraphdata=downloadgraphdata+Dataval[2]+" (1)";
                    }
                    cIndex=chkcol(Dataval[1]);

                        if (cIndex==false)
                            {
                                speciessum=parseFloat(Dataval[3]);
                                dataBar.setCell(clusCounter, addcolum(Dataval[1]),speciessum);
                                downloadgraphdata=downloadgraphdata+":"+Dataval[1]+":"+speciessum+"@@";


                            }
                        else
                            {
                                if (dataBar.getValue(clusCounter,cIndex)!=null)
                                    {
                                        speciessum=parseFloat(Dataval[3])+parseFloat(dataBar.getValue(clusCounter,cIndex));
                                    }
                                    else
                                        {
                                            speciessum=parseFloat(Dataval[3]);
                                        }

                                dataBar.setCell(clusCounter,cIndex ,speciessum);
                                downloadgraphdata=downloadgraphdata+":"+Dataval[1]+":"+speciessum+"@@";
                            }
                      }
                }
            }

$("#btndownloadbiographdata").click (function(){
  location.href = "http://" + location.hostname + "/downloaddata/downloadgraphdata?search="+downloadgraphdata+"&tag=BIO";
});


         chart = new google.visualization.ColumnChart(document.getElementById('GraphBio_Bar'));
         google.visualization.events.addListener(chart, 'onmouseover', function(e) {




                delGraphMarker();
                //zoomLocation(dataArray[e.row]);
                
               var t=dataArray[e.row]+"";
                 t=t.split(",");
                t[0]= t[0].substr(1);
                t[1]= t[1].substring(0,t[1].length-1);
        var myLatlng = new google.maps.LatLng(t[0],t[1]);
        BioGraphMarker = new google.maps.Marker({
          position: myLatlng,
          map: map,
          icon:"/images/target.png"
        });





    });

        chart.draw(dataBar, {
chartArea:{left:50,top:15,width:"95%",height:document.getElementById("GraphBio_Bar").style.height},
fontSize: 10,
gridlineColor:	"#dbe6ec",
legend:'bottom',
legendTextStyle: {color: '#5375ab'},
isStacked:'true',
vAxis: {title: 'Numbers',titleTextStyle: {color: '#5375ab'},textStyle:{color:'#5375ab'}},
backgroundColor: {strokeWidth:0},
axisTitlesPosition:'out',
hAxis:{title:'Locations',titleTextStyle: {color: '#5375ab'},textStyle:{color:txtColor,fontSize:fntSize}}
                         });



});




}
function SetNonBioChart1(params)
{



var downloadgraphdata="";
if(clusterdata.length<=0 ||  $("#panel_detail").is(":visible")==false)
     return;
 $("#GraphBio_Bar").html('<div style="height: 100%; width: 100%; background: url(/images/loading/loading9.gif) 45% 50% no-repeat">  </div>');

delGraphMarker();

var clusterlocations="";
    for(var clusCounter=0; clusCounter<=clusterdata.length-1;clusCounter++)
        {
           if (clusterlocations!="")
               {
                   clusterlocations=clusterlocations+",";
               }
           clusterlocations=clusterlocations+clusterdata[clusCounter].title;
        }


    var ul="";
            ul="http://" + location.hostname + "/visualization/Nonbiographdata?search=" + clusterlocations + "&Selectedattribute="+params;
    downloadUrl(url = ul, function(returnVal) {

        var rawData = returnVal.split("|")
        
        var DataVal=Array();
        var Index=0;
        var tmpArray;
        var locChk="";
        var tmpIndex=0;
       for(var clusCounter=0; clusCounter<=rawData.length-1;clusCounter++)
          {
            
                    
            var tmpData=rawData[clusCounter].split(":");
            
            if (locChk!=tmpData[0])
                {Index=0;
                    tmpIndex=(DataVal.length-1+1);
                    tmpArray=new Array();
                    tmpArray[Index]= parseFloat(tmpData[3]);
                    DataVal[tmpIndex]=new Array(5);
                    DataVal[tmpIndex][0]=tmpData[2];
                    DataVal[tmpIndex][1]=array_min(tmpArray);
                    DataVal[tmpIndex][2]=array_firstQuartile(tmpArray);
                    DataVal[tmpIndex][3]=array_thirdQuartile(tmpArray);
                    DataVal[tmpIndex][4]=array_max(tmpArray);
                    locChk=tmpData[0];
                }
            else
                {
                    Index++;
                    tmpArray[Index]= parseFloat(tmpData[3]);


                    DataVal[tmpIndex][1]=array_min(tmpArray);
                    DataVal[tmpIndex][2]=array_firstQuartile(tmpArray);
                    DataVal[tmpIndex][3]=array_thirdQuartile(tmpArray);
                    DataVal[tmpIndex][4]=array_max(tmpArray);
                    locChk=tmpData[0];

                }
          }
$("#btndownloadbiographdata").click (function(){
  location.href = "http://" + location.hostname + "/downloaddata/Downloadgraphdata?search="+downloadgraphdata+"&tag=NONBIO";
});

var wd=$("#panel_detail").width();
    
 var dataTable = google.visualization.arrayToDataTable(DataVal, true);
  
        var chart = new google.visualization.ImageCandlestickChart(document.getElementById('GraphBio_Bar'));
        chart.draw(dataTable, {width: 1000});
    

});


}
function SetNonBioChart(params)
{//alert(clusterdata.length);
   var downloadgraphdata="";
 if(clusterdata.length<=0 ||  $("#panel_detail").is(":visible")==false)
     return;
 $("#GraphBio_Bar").html('<div style="height: 100%; width: 100%; background: url(/images/loading/loading9.gif) 45% 50% no-repeat">  </div>');

delGraphMarker();

var clusterlocations="";
    for(var clusCounter=0; clusCounter<=clusterdata.length-1;clusCounter++)
        {
           if (clusterlocations!="")
               {
                   clusterlocations=clusterlocations+",";
               }
           clusterlocations=clusterlocations+clusterdata[clusCounter].title;
        }


    var ul="";
            ul="http://" + location.hostname + "/visualization/Nonbiographdata?search=" + clusterlocations + "&Selectedattribute="+params;
    downloadUrl(url = ul, function(returnVal) {

        var rawData = returnVal.split("|");

         dataBar = new google.visualization.DataTable();
         dataBar.addColumn('string', 'Location');
         var chkRow="";
         var counter=-1;
         var vAxisName="";
        var cIndex;
        var Dataval;
        var speciessum=0;
        var fntSize=10;
        var txtColor="#5375ab";
        var tmpLocationIds;
        var dataArray=new Array();
        for(var clusCounter=0; clusCounter<=clusterdata.length-1;clusCounter++)
            {if (downloadgraphdata!="")downloadgraphdata=downloadgraphdata+"|";
                tmpLocationIds=clusterdata[clusCounter].title.split(",");

                dataBar.addRow();
                if (clusterdata[clusCounter].getMarkers().length==1)
                    {
                        //dataBar.setCell(clusCounter, 0,clusterdata[clusCounter].title+"");
                    }
                else if (clusterdata[clusCounter].getMarkers().length>1)
                    {
                        dataBar.setCell(clusCounter, 0,clusterTitle[clusCounter],"Location: "+clusterTitle[clusCounter]);
                        dataArray[clusCounter]=clusterdata[clusCounter].getCenter();
                        downloadgraphdata=downloadgraphdata+clusterTitle[clusCounter];


                    }


               for(var rdata=0; rdata<=rawData.length-2;rdata++)
                {
                  Dataval = rawData[rdata].split(":");
                  if(indexofArray(tmpLocationIds,Dataval[0])!= -1)
                      {
                     if (clusterdata[clusCounter].getMarkers().length==1)
                    {
                        dataBar.setCell(clusCounter, 0,Dataval[2]+" (1)","Location: "+Dataval[2]);
                        dataArray[clusCounter]=clusterdata[clusCounter].getCenter();
                        downloadgraphdata=downloadgraphdata+Dataval[2]+" (1)";
                    }
                    vAxisName=Dataval[1]
                    cIndex=chkcol(Dataval[9]);

                        if (cIndex==false)
                            {
                                speciessum=parseFloat(Dataval[3]);
                                dataBar.setCell(clusCounter, addcolum(Dataval[9]),speciessum,"\n"+"Depth: "+Dataval[9]+"\n"+vAxisName+": "+Dataval[3]);
                                downloadgraphdata=downloadgraphdata+":"+Dataval[9]+":"+speciessum+"@@";


                            }
                        else
                            {
                                if (dataBar.getValue(clusCounter,cIndex)!=null)
                                    {
                                        //speciessum=parseFloat(Dataval[3])+parseFloat(dataBar.getValue(clusCounter,cIndex));
                                    }
                                    else
                                        {speciessum=parseFloat(Dataval[3]);
                                            dataBar.setCell(clusCounter,cIndex ,speciessum,"\n"+"Depth: "+Dataval[9]+"\n"+vAxisName+": "+Dataval[3]);
                                            downloadgraphdata=downloadgraphdata+":"+Dataval[1]+":"+speciessum+"@@";
                                        }

                                
                                
                            }
                      }
                }
            }

$("#btndownloadbiographdata").click (function(){
  location.href = "http://" + location.hostname + "/downloaddata/Downloadgraphdata?search="+downloadgraphdata+"&tag=NONBIO";
});


         chart = new google.visualization.ColumnChart(document.getElementById('GraphBio_Bar'));
         google.visualization.events.addListener(chart, 'onmouseover', function(e) {




                delGraphMarker();
                //zoomLocation(dataArray[e.row]);

               var t=dataArray[e.row]+"";
                 t=t.split(",");
                t[0]= t[0].substr(1);
                t[1]= t[1].substring(0,t[1].length-1);
        var myLatlng = new google.maps.LatLng(t[0],t[1]);
        BioGraphMarker = new google.maps.Marker({
          position: myLatlng,
          map: map,
          icon:"/images/target.png"
        });





    });

        chart.draw(dataBar, {
chartArea:{left:50,top:15,width:"95%",height:document.getElementById("GraphBio_Bar").style.height},
fontSize: 10,
gridlineColor:	"#dbe6ec",
legend:'bottom',
legendTextStyle: {color: '#5375ab'},
isStacked:'false',
vAxis: {title: vAxisName,titleTextStyle: {color: '#5375ab'},textStyle:{color:'#5375ab'}},
backgroundColor: {strokeWidth:0},
axisTitlesPosition:'out',
hAxis:{title:'Locations (Depth)',titleTextStyle: {color: '#5375ab'},textStyle:{color:txtColor,fontSize:fntSize}}
                         });



});




}




function chkcol(cname)
{
    for (var a=0; a<=dataBar.getNumberOfColumns()-1;a++)
        {
            if (dataBar.getColumnLabel(a)==cname)
                {
                    return a;
                }
        }
        return false;

}



function addcolum(spname)
{
    return dataBar.addColumn('number', spname);
}
function delGraphMarker()
{
    if (BioGraphMarker!=null)
            {
        BioGraphMarker.setMap(null);

        BioGraphMarker = null;
            }
}


function indexofArray(Array,obj)
{
   
  
   for(var i=0; i<Array.length; i++){
    if(Array[i]==obj){
     return i;
    }
   }
   return -1;
}